package biz.datalk.industrialland.admin.modules.util;

import biz.datalk.industrialland.admin.component.shiro.CustomLoginUser;
import biz.datalk.industrialland.common.exception.ServiceException;
import biz.datalk.industrialland.mbg.pojo.po.SysUser;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.SecurityUtils;

/**
 * @author tarofang@163.com
 * @date 2020-08-26
 */

@Slf4j
public class ShiroUtil {

    public static CustomLoginUser currentLoginUser() {
        Object obj = SecurityUtils.getSubject().getPrincipal();
        if (!(obj instanceof CustomLoginUser)) {
            log.warn("shiro getPrincipal is null");
            throw new ServiceException("您未登录或长时间未操作");
        }
        return (CustomLoginUser) obj;
    }

    public static SysUser currentSysUser() {
        CustomLoginUser customLoginUser = currentLoginUser();
        return customLoginUser.getSysUser();
    }

    // public static biz.datalk.industrialland.admin.modules.system.pojo.po.SysUser getCurrentUser() {
    //     SysUser sysUser = currentSysUser();
    //     biz.datalk.industrialland.admin.modules.system.pojo.po.SysUser resSysUser = new biz.datalk.industrialland.admin.modules.system.pojo.po.SysUser();
    //     BeanUtils.copyProperties(sysUser, resSysUser);
    //     return resSysUser;
    // }

    public static Long getCurrentUserId() {
        return currentLoginUser().getId();
    }

}
